#!/usr/bin/perl

require "./accounts.pl";

&ReadParse();

my ($username, $uid, $error_msg, %user);

$error_msg = "";

if($in{"username"})
{
	$username = $in{"username"};
}
else
{
	$error_msg = $text{'error_msg_no_username'};
}

unless($error_msg)
{
	$uid = &local_user_exist($username);
	if($uid < 0)
	{		
		$error_msg = $text{'error_msg_user_not_exist_local'};
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		if($in{'disabled'} eq "no")
		{
			$event = $text{'accounts_naslog_event_disable_fail_uname_not_exist'};
			$formatstr = sprintf $text{'accounts_naslog_disable_fail_uname_not_exist'}, $username;
		}
		else
		{
			$event = $text{'accounts_naslog_event_enable_fail_uname_not_exist'};
			$formatstr = sprintf $text{'accounts_naslog_enable_fail_uname_not_exist'}, $username;
		}
	}
}

if($error_msg)
{
#	&header($text{'index_title'}, "", undef, 0, 1);

#	print "<p>", $error_msg;
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	
	&error_setup($text{'error_title_disable_user'});

	&error($error_msg);

#	&footer("/accounts/", $text{'index'});
}
else
{
#	&header($text{'index_title'}, "", undef, 0, 1);
	if($in{'disabled'} eq "no")
	{
		&protect_shadow();
		&disable_user($username);
		&unprotect_shadow();
		$logminor= $text{'module_name'};

		$logpriority=$text{'naslog_info'};
		$event = $text{'accounts_naslog_event_disable_success'};
		$formatstr = sprintf $text{'accounts_naslog_disable_success'}, $username;
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	elsif($in{'disabled'} eq "yes")
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_info'};
		$event = $text{'accounts_naslog_event_enable_success'};
		$formatstr = sprintf $text{'accounts_naslog_enable_success'}, $username;
		&nas_syslog($logminor, $logpriority, $event, $formatstr);

		&protect_shadow();
		&enable_user($username);
		&unprotect_shadow();
	}
	&redirect("index.cgi");
#	&footer("/accounts/", $text{'index'});
}

